﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Rss;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_ins_RssNews_RssLinkInput : System.Web.UI.UserControl
{
    public RssSite _Site = null;
    public RssLink _Link = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        try
        {
            int siteID = WebGlobal.GetSite().ID;
            int langID = LanguageUtility.GetLanguageID();

            _Site = new RssSite(Convert.ToInt32(Request.QueryString["site"].Trim()));
            if (_Site.ID < 1)
            {
                throw new Exception("Chức năng không hợp lệ.");
            }

            if (Request.QueryString["id"] != null)
            {
                _Link = new RssLink(Convert.ToInt32(Request.QueryString["id"].Trim()));
                if (_Link.ID < 1)
                {
                    throw new Exception("Chức năng không hợp lệ.");
                }

                if (!this.IsPostBack)
                {
                    txtName.Text = _Link.Name;
                    txtURL.Text = _Link.RssUrl;
                    txtFrequency.Text = _Link.Frequency.ToString();
                    chkActive.Checked = _Link.Activated;
                }
            }
        }
        catch //(Exception ex)
        {
            Response.Redirect("?type=invalidurl&ReturnUrl=" + Server.UrlEncode(Request.RawUrl), false);
            return;
            //HtmlGenericControl ctrl = new HtmlGenericControl("div");
            //ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            //this.Controls.Add(ctrl);
        }
    }

    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            RssLink link = null;
            if ("addlink".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                link = new RssLink();
                link.LangID = LanguageUtility.GetLanguageID();
                link.LastSync = DateTime.MaxValue;
            }
            else if ("editlink".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                link = new RssLink(Convert.ToInt32(Request.QueryString["id"].Trim()));
            }

            link.RssSiteID = _Site.ID;
            link.Name = txtName.Text.Trim();
            link.RssUrl = txtURL.Text.Trim();
            link.Frequency = Convert.ToInt32(txtFrequency.Text.Trim());
            link.Activated = chkActive.Checked;

            if ("addlink".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                link.Insert();
            }
            else if ("editlink".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                link.Update();
            }
            else
            {
                throw new Exception("This function is not supported.");
            }

            string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + (Request.QueryString["site"] == null ? "" : "&site=" + Request.QueryString["site"]);
            Response.Redirect(bUrl, false);
            return;
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            this.Controls.Add(ctrl);
        }
    }
}
